package com.trt.user.service.impl;

import com.trt.enums.ResponseStatusEnum;
import com.trt.enums.Sex;
import com.trt.enums.UserStatus;
import com.trt.exception.ApiException;
import com.trt.pojo.dto.AppUserBO;
import com.trt.utils.*;
import com.trt.pojo.AppUser;
import com.trt.user.mapper.AppUserMapper;
import com.trt.user.service.IUserService;
import org.n3r.idworker.Sid;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;

/**
 * @author tanrentao
 * @date 2021-12-02
 * @desc
 */
@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    private AppUserMapper appUserMapper;

    @Autowired
    private Sid sid;

    @Autowired
    public RedisUtils redisUtils;

    private static final String REDIS_USER_INFO = "redis_user_info";

    private static final String DEFAULT_USER_FACE0 = "http://122.152.205.72:88/group1/M00/00/05/CpoxxFw_8_qAIlFXAAAcIhVPdSg994.png";
    private static final String DEFAULT_USER_FACE1 = "http://122.152.205.72:88/group1/M00/00/05/CpoxxF6ZUySASMbOAABBAXhjY0Y649.png";
    private static final String DEFAULT_USER_FACE2 = "http://122.152.205.72:88/group1/M00/00/05/CpoxxF6ZUx6ANoEMAABTntpyjOo395.png";

    @Override
    public AppUser selectAppUserIsExistByMobile(String mobile) {
        Example userExample = new Example(AppUser.class);
        Example.Criteria userCriteria = userExample.createCriteria();
        userCriteria.andEqualTo("mobile", mobile);
        return appUserMapper.selectOneByExample(userExample);
    }

    @Transactional
    @Override
    public AppUser createUser(String mobile) {
        String userId = sid.nextShort();

        AppUser appUser = new AppUser();
        appUser.setId(userId);
        appUser.setMobile(mobile);
        appUser.setNickname("用户：" + DesensitizationUtil.commonDisplay(mobile));
        appUser.setFace(DEFAULT_USER_FACE0);
        appUser.setBirthday(DateUtil.stringToDate("1900-01-01"));
        appUser.setSex(Sex.secret.type);
        appUser.setActiveStatus(UserStatus.INACTIVE.type);
        appUser.setTotalIncome(0);
        appUser.setCreateTime(new Date());
        appUser.setUpdateTime(new Date());

        appUserMapper.insert(appUser);

        return appUser;
    }

    @Override
    public AppUser selectUser(String userId) {
        return appUserMapper.selectByPrimaryKey(userId);
    }

    @Transactional
    @Override
    public void updateAppUser(AppUserBO appUserBO) {
        String userId = appUserBO.getId();

        // 保证双写一致，先删除redis中的数据，后更新数据库
        redisUtils.del(REDIS_USER_INFO + ":" +userId);

        AppUser appUser = new AppUser();
        BeanUtils.copyProperties(appUserBO, appUser);
        appUser.setUpdateTime(new Date());
        appUser.setActiveStatus(UserStatus.ACTIVE.type);

        int result = appUserMapper.updateByPrimaryKeySelective(appUser);
        if (result != 1) {
            ApiException.display(ResponseStatusEnum.USER_UPDATE_ERROR);
        }

        // 再次查询用户的最新信息，放入redis中
        AppUser newAppUser = selectUser(userId);
        redisUtils.set(REDIS_USER_INFO + "：" + userId, JsonUtils.objectToJson(newAppUser));

        // 缓存双删策略
        try {
            Thread.sleep(100);
            redisUtils.del(REDIS_USER_INFO + ":" +userId);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
